/*Author: Ilana M Ventura														  */
/*Date: September 2024															  */
/*Step 4: Table S1. Comparison of PERI sample with the 2021 American 			  */
/*					Community Survey	 										  */
/*		  Table S2. Randomization Check: Predicting Assignment to 				  */
/*					Treatment Condition		 									  */



/****Table S1. Comparison of PERI sample with the 2021 American Community Survey***/
/*NOTE: Data and code to replicate ACS data not in packadge, as it is public use 
		and can be obtained from IPUMS. See 2021 ACS data, variables: year, hispan,
		sex, educ, age, RACASIAN ,RACBLK, RACWHT, perwt.
*/

*Recode hispanic and non-hisp black, white, asian;
Data sl_dat;
	set sl_dat;
	if hisp=0 then do;
		if white=1 then NHWhite=1;
		if black=1 then NHBlack=1;
		if Asian=1 then NHAsian=1;
			end;
	if NHWhite=. then NHWhite=0;
	if NHBlack=. then NHBlack=0;
	if NHAsian=. then NHAsian=0;
run;

proc freq data= sl_dat;
	table hisp*white*NHWhite
			hisp*black*NHBlack
			hisp*asian*NHAsian/ list missing norow nocol;
run;
	

/*Total pop estimates, by race group*/
proc means data= sl_dat nway noprint;
	weight weight_genB_13;
	var NHWhite hisp NHBlack NHAsian ;
	output out= PERI_race (drop = _type_ _freq_) Mean= StdErr= /autoname;
run;

proc transpose data=PERI_race out= PERI_race; run;

Data PERI_race;
	set PERI_race;
	
	col1_a=round(col1,.0001);
	
	Var=scan(_NAME_, 1, '_');
    Type=scan(_NAME_, 2, '_');
    
	If Type= "StdErr" then Est=cats("(",col1_a*100,")");
		else Est=cats(col1_a*100);
	drop _name_ col1 col1_a;
run;


*Loop over demographic variables;
%macro loop(varlist);
%let i=1;
%do %while (%scan(&varlist, &i, |) ^=%str());
%let var=%scan(&varlist, &i, |); 
%put &var;


proc means data= sl_dat nway noprint;
	where &Var=1;
	weight weight_genB_13;
	var  age_grp_1 age_grp_2 age_grp_3 age_grp_4
		Educ_LT_HS Educ_HS Educ_college Female ;
	output out= PERI_&Var (drop = _type_ _freq_) Mean= StdErr= /autoname;
run;

proc transpose data=PERI_&Var out= PERI_&Var; run;

Data PERI_&Var;
	set PERI_&Var;
	
	col1_a=round(col1,.0001);
	
    Type=scan(_NAME_, -1, '_');
    n= length(_NAME_) - length(scan(_NAME_, -1, '_') );
    
	var=substr(_NAME_, 1, n-1);

    
	If Type= "StdErr" then Est=cats("(",col1_a*100,")");
		else Est=cats(col1_a*100);
	drop _name_ col1 col1_a n;
run;

proc sort data= PERI_&Var; by var type; run;

data PERI_&Var;
	set PERI_&Var;
	n+1;
	if var = "Educ_HS" then n2=n+2;
		else if var = "Educ_LT_HS" then n2=n-2;
		else n2=n;
	drop n;
run;

proc sort data= PERI_&Var; by n2 ; run;

Data PERI_&Var;
	set PERI_race (in=a) PERI_&Var;
	if a and var ne "&Var" then delete;
	if n2=. then n2=0; 
	rename est=&Var;
	if type="Mean" then do;
	if var="&Var" then Var2= "Population size";
		else if var="Educ_LT_HS" then var2="Educ: LT HS";
		else if var="Educ_HS" then var2="Educ: HS";
		else if var="Educ_college" then var2="Educ: College";
		else if var="age_grp_1" then var2="Age: 18-29";
		else if var="age_grp_2" then var2="Age: 30-44";
		else if var="age_grp_3" then var2="Age: 45-64";
		else if var="age_grp_4" then var2="Age: 65+";
		else if var="R_US_Born" then var2="US Born";
		else var2=var;
	end;
run;

*Increment counter;
%let i=%eval(&i+1);
%end;
%mend;
%let temp=NHWhite|Hisp|NHBlack|NHAsian;
%loop(&temp);


Data PERI_comb;
	retain Var2;
	merge PERI_nhwhite PERI_hisp PERI_nhblack PERI_nhasian;
		by n2 type;
	label NHWhite="Non-Hispanic Whites";
	label Hisp="Hispanics";
	label NHBlack= "Non-Hispanic African-Americans";
	label NHAsian= "Non-Hispanic Asians";
	drop var type n2;
run;

proc print data= PERI_comb noobs label; run;



/********Table S2. Randomization Check: Predicting Assignment to Treatment Condition*****/


*Model;
proc logistic data = sl_dat desc ;
class  filter_rand / param = ref;
model filter_rand =   age_grp_2 age_grp_3 age_grp_4 Female Speak_other_language skin_med skin_dark
					R_US_Born Educ_college Home_owned Inc_d 
					republican/rsq ;

ods output NObs=Mod_N_Rand FitStatistics=Mod_fit_Rand RSquare=Mod_Rsq_Rand
	ParameterEstimates= Mod_est_Rand OddsRatios=Mod_odds_Rand ModelInfo=Mod_Info_Rand
	Association=Mod_Ascn_Rand;
run;

*Format table;

%macro loop(varlist);
%let i=1;
%do %while (%scan(&varlist, &i, |) ^=%str());
%let var=%scan(&varlist, &i, |); 
%put &var;


%let model= Mod_est_&var;
%let fit= Mod_fit_&var;
%let num= Mod_N_&var;
%let rsq= Mod_Rsq_&var;
%let VarName= Mod_Info_&var;
%let ascn= Mod_Ascn_&var;

Data &VarName;
	set &VarName;
	if description="Response Variable";
	CALL SYMPUT('DepVar',value) ;
run;
%put &DepVar &var;


/*put global macros for starting model and output name*/
%put &model &fit &num &rsq ;


*full model;


Data Mod;
	set &model ;
	length stars $5. ClassVal0 $30.;
	if strip(ProbChiSq) <.0001 then stars="***";
		else if ProbChiSq <.001 then stars="**";
		else if ProbChiSq <.05 then stars="*";
	n+1;
	if ClassVal0="" then ClassVal0="";
	keep n Variable estimate StdErr stars   ClassVal0 ;

run;


proc transpose data= Mod out=Mod_t;
	var estimate StdErr;
	by n Variable stars  ClassVal0;
run;

Data Mod_t;
	set Mod_t;
	col1_a=round(col1,.001);
	Variable_merge=Variable;
	If _NAME_= "Estimate" then Model_&var=cats(col1_a,stars);
		else if  _NAME_= "StdErr" then do;
			Model_&var=cats("(",col1_a,")");
			Variable="";
		end;
	if _NAME_= "StdErr" then n2=n+.5;
		else n2=n;
	drop _LABEL_  COL1 stars col1_a n;
run;
	
	
Data Mod_n;
	length Model_&var $8.;
	set &num (rename=(Label= Variable  ))
		&fit (rename=(Criterion= Variable))
		&rsq (rename=(Label2=Variable))
		&ascn(rename=(Label1=Variable));
	if Variable in ("Number of Observations Used", "AIC", "Percent Concordant") or Label1 in ("R-Square");
	
	if Model_&var= "" then do;
		if InterceptAndCovariates ne . then Model_&var=InterceptAndCovariates;
		else if Variable ="Max-rescaled R-Square" and cValue2 ne . then Model_&var=cValue2;
		else if NobsUsed ne . then  Model_&var=NobsUsed;
		else if Variable ="Percent Concordant" and cvalue1 ne . then  Model_&var=cvalue1;
	end;
	n2=40;
	keep Variable Model_&var _name_ variable_merge n2;
run;

Data Mod_out_&Var;
 	length Variable  $200.;
	set Mod_t Mod_n;
run;



*Increment counter;
%let i=%eval(&i+1);
%end;
%mend;
%let temp=rand;
%loop(&temp);


proc print data= Mod_out_rand noobs; run;


